<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of home
 *
 * @author Eko Prasetyo<nir@nirleka.web.id>
 */
class login extends GON_Public {

	function index() {
		$this->__check_login();
		$data['title'] = "Login Page";
		$this->__declare_view($data, "login");
	}

	public function __check_login() {
		//check login
		if ($this->session->userdata('logged_in')) {
			redirect('utama');
		}
	}

	function proses() {
		$this->__check_login();
		if (!empty($_POST)) {
			$data = array(
				'username' => $this->input->post('username'),
				'password' => $this->input->post('password'),
			);
			$res = $this->db
					->select('idPegawai, username, nama_pegawai, jabatan')
					->where('username', $data['username'])
					->where('password', $data['password'])
					->from('pegawai')
					->get();
			if ($res->num_rows == 1) {
				$result = $res->row_array();
				$jabatan = ucwords($result['jabatan']);
				$jabatan = str_replace("_", " ", $jabatan);
				$logged_data = array(
					'idPegawai' => $result['idPegawai'],
					'username' => $result['username'],
					'logged_in' => true,
					'jabatan' => $jabatan
				);
				$this->session->set_userdata($logged_data);
				redirect('utama');
			} else {
				$this->flash_message->error('Username atau password tidak terdaftar');
				redirect('login');
			}
		} else {
			$this->flash_message->error('Username atau password belum diisi');
			redirect('login');
		}
	}

	function logout() {
		$this->session->sess_destroy();
		$this->session->sess_create();
		$this->flash_message->success('Logout Success');
		redirect('login');
	}

	function err404() {
		$this->load->view('err404');
	}

}

//end of file